package com.tool_stdy.Spider;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class SimulationLogin {

    public static void getCookie() throws Exception {
        String url = "https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn";
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", "****"));
        list.add(new BasicNameValuePair("password", "***"));
        list.add(new BasicNameValuePair("lt", "LT-179123-YlVefQKmq327iE9nGnyRKHsV6siB4W"));
        list.add(new BasicNameValuePair("execution", "e3s1"));
        list.add(new BasicNameValuePair("_eventId", "submit"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
        httpPost.setEntity(entity);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpResponse response = client.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 302) {
            String newUrl = response.getLastHeader("Location").getValue();
            newUrl = newUrl.split("from=")[1];
            System.out.println(newUrl);
            HttpGet get = new HttpGet(newUrl);
            HttpResponse responseGet = client.execute(get);
            System.out.println(EntityUtils.toString(responseGet.getEntity(), "utf-8"));
        }
        HttpEntity ResEntity = response.getEntity();
        System.out.println(EntityUtils.toString(ResEntity, "utf-8"));
    }

    public static void main(String[] args) throws Exception {
        getCookie();
    }
}
